package com.tianma315.admin.agent.rest;


import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.agent.domain.AgentDO;
import com.tianma315.admin.agent.service.AgentService;
import com.tianma315.admin.agent.vo.AgentVO;
import com.tianma315.admin.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <pre>
 * 经销商
 * </pre>
 * <small> 2019-05-21 15:57:56 | zcm</small>
 */
@RestController
@RequestMapping("/agent")
public class AgentRestController {
    @Autowired
    private AgentService agentService;

    @GetMapping("/list")
    @RequiresPermissions("agent:list")
    public Result<Page<AgentDO>> list(Integer pageNumber, Integer pageSize, String searchKey) {
        // 查询列表数据
        Page<AgentDO> page = agentService.getPage(pageNumber, pageSize, searchKey);
        return Result.ok(page);
    }

    @GetMapping("/list/enable")
    @RequiresPermissions("agent:list")
    public Result<Page<AgentDO>> listEnable(Integer pageNumber, Integer pageSize, String searchKey) {
        // 查询列表数据
        Page<AgentDO> page = agentService.getEnablePage(pageNumber, pageSize, searchKey);
        return Result.ok(page);
    }

    /**
     * 添加经销商
     *
     * @param agent
     * @return
     */
    @PostMapping("/save")
    @RequiresPermissions("agent:add")
    public Result<String> save(AgentVO agent) {
        if (agentService.save(agent)) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 修改 修改经销商
     *
     * @param agent
     * @return
     */
    @RequestMapping("/edit")
    @RequiresPermissions("agent:edit")
    public Result<String> edit(AgentVO agent) {
        if (agentService.edit(agent))
            return Result.ok();
        return Result.fail();
    }

    /**
     * 冻结/激活经销商
     *
     * @param agentId
     * @param status
     * @return
     */
    @PostMapping("/status/change")
    @RequiresPermissions("agent:edit")
    public Result<String> change(Long agentId, int status) {
        if (agentService.changeStatus(agentId, status)) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 查找所有可用经销商
     */
    @GetMapping("/getAgentList")
    public Result<List<AgentDO>> getAgentList() {
        List<AgentDO> list = agentService.getAgentList();
        return Result.ok(list);
    }

}
